package com.spar.openprojects.shared.rowmapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import com.spar.openprojects.shared.BusinessPartner;
import com.spar.openprojects.shared.Member;
import com.spar.openprojects.shared.Order;
import com.sql.database.IRowMapper;

public class OrderRowMapper implements IRowMapper {

	@Override
	public Order mapRow(ResultSet rs) throws SQLException {
		Order order = new Order();

		order.setId(rs.getInt("o.id"));
		if (rs.getTimestamp("o.updated") != null) {
			order.setCreated(new Date(rs.getTimestamp("o.updated").getTime()));
		}
		if (rs.getTimestamp("o.created") != null) {
			order.setCreated(new Date(rs.getTimestamp("o.created").getTime()));
		}
		BusinessPartner businessPartner = new BusinessPartner();
		BusinessPartnerRowMapper businessPartnerRowMapper = new BusinessPartnerRowMapper();
		businessPartner = businessPartnerRowMapper.mapRow(rs);

		order.setBusinessPartner(businessPartner);
		order.setProjectId(rs.getInt("o.project_id"));
		Integer everyYear = rs.getInt("o.every_year");
		if (everyYear == 0)
			order.setEveryYear(false);
		else
			order.setEveryYear(true);

		Member updateMember = new Member();
		updateMember.setNumber(rs.getString("o.member_number_updated"));
		order.setUpdateMember(updateMember);
		Member createMember = new Member();
		createMember.setNumber(rs.getString("o.member_number_created"));
		order.setCreateMember(createMember);

		return order;
	}

}
